package org.lk.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.lk.reggie.dto.DishDto;
import org.lk.reggie.entity.Dish;
import org.lk.reggie.entity.DishFlavor;
import org.lk.reggie.mapper.DishMapper;
import org.lk.reggie.service.DishFlavorService;
import org.lk.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;
    /**
     * 新增菜品，同时保存对应的口味数据
     * @param dishDto
     */
    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto)
    {
        // 保存菜品的基本信息到菜品表
        this.save(dishDto);
        // 保存菜品口味到菜品口味表 dish_flavor
        Long id = dishDto.getId();

        List<DishFlavor> flavors = dishDto.getFlavors();

        for (DishFlavor dis:
             flavors)
        {
            dis.setDishId(id);
        }

        dishFlavorService.saveBatch(flavors);

    }

    @Override
    public DishDto getDishById(Long id)
    {
        Long dishId = id;
        Dish dishById = this.getById(id);
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor:: getDishId, dishId);
        List<DishFlavor> list = dishFlavorService.list(queryWrapper);
        DishDto dto = new DishDto();
        BeanUtils.copyProperties(dishById, dto);
        dto.setFlavors(list);
        return dto;
    }

    @Override
    @Transactional
    public boolean myUpdateDish(DishDto dishDto)
    {
        // 更新菜品的基本信息到菜品表
        boolean b = this.updateById(dishDto);
        // 保存菜品口味到菜品口味表 dish_flavor
        Long id = dishDto.getId();

        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor :: getDishId, id);

        dishFlavorService.remove(queryWrapper);

        List<DishFlavor> flavors = dishDto.getFlavors();

        for (DishFlavor dis:
                flavors)
        {
            dis.setDishId(id);
        }

        dishFlavorService.saveBatch(flavors);

        return b ;
    }

    @Override
    @Transactional
    public boolean suspend(String ids, Integer status)
    {
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();

        String[] manyId = ids.split(",");
        for (String id:
             manyId)
        {
            Long str2Int = Long.parseLong(id);
            queryWrapper.eq(Dish :: getId, str2Int);
            Dish dish = this.getById(str2Int);
            dish.setStatus(status);
            this.updateById(dish);
        }

        return true;
    }

    @Override
    public boolean deleteDish(Long id)
    {

        LambdaQueryWrapper<DishFlavor> dishFlavorQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorQueryWrapper.eq(DishFlavor :: getDishId, id);
        boolean remove = dishFlavorService.remove(dishFlavorQueryWrapper);

        LambdaQueryWrapper<Dish> dishQueryWrapper = new LambdaQueryWrapper<>();
        dishQueryWrapper.eq(Dish :: getId, id);
        boolean remove1 = this.remove(dishQueryWrapper);

        return remove && remove1;
    }
}
